'''
Author: your name
Date: 2021-11-06 20:06:38
LastEditTime: 2021-11-12 11:57:19
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: /tinyms/tinyms-api/apps/cms/channel/service.py
'''
from app import db
from .model import Channel
from .channelDto import ChannelDto

import json

class ChannelService:
    '''
    保存数据
    '''
    def save(channel):
        db.session.add(channel)
        db.session.commit()
    
    '''
    删除数据
    '''
    def delete(channel_pk):
        channel = Channel.query.get(channel_pk)
        db.session.delete(channel)
        db.session.commit()
    '''
    修改数据
    '''
    def update(channel):
        tmp_channel = Channel.query.get(channel.channel_pk)
        tmp_channel.channel_name = channel.channel_name
        tmp_channel.channel_parent = channel.channel_parent
        tmp_channel.channel_english = channel.channel_english
        tmp_channel.channel_path = channel.channel_path
        tmp_channel.channel_content_template = channel.channel_content_template
        tmp_channel.channel_page_template = channel.channel_page_template
        tmp_channel.channel_page_count = channel.channel_page_count
        tmp_channel.channel_list_template = channel.channel_list_template
        tmp_channel.channel_list_count = channel.channel_list_count
        tmp_channel.channel_state = channel.channel_state
        db.session.commit()
    
    '''
    分页查询
    '''
    def pages(page):
        pages = Channel.query.paginate(int(page),30,False)
        return pages
    
    '''
    查询所有菜单列表(包含按钮)
    '''
    def list(self,channel_state):
        #查询父菜单
        channels = []
        channel = ChannelDto(None,None)
        #调用递归方法获取菜单集合
        rootChannels = self.recursive_list(channel_state,'0',channel,channels)
        #将菜单集合转化为json格式
        # channels_json = json.dumps(rootChannels, ensure_ascii=False,default=lambda o: o.__dict__, sort_keys=True)
        channels_json = json.dumps(rootChannels, ensure_ascii=False,default=lambda o: o.__dict__)
        return channels_json
    
    '''
    递归查询栏目
    type为菜单类型 1菜单
    '''
    def recursive_list(self,state,pk,channeldto,channels):
        # 记录子菜单列表
        children = []
        #查询父菜单
        if state is not None:
            parents = Channel.query.filter(Channel.channel_state==state,Channel.channel_parent==pk).all()
        else:
            parents = Channel.query.filter(Channel.channel_parent==pk).all()
        #遍历父菜单查询对应子菜单
        for channel in parents:
            #临时菜单实体
            tmp_channeldto = ChannelDto(channel,None)
            #如果是第一级菜单
            if channel.channel_parent == '0':
                #将菜单加入到list中
                channels.append(tmp_channeldto)
                #继续遍历下级菜单
                self.recursive_list(state,channel.channel_pk,tmp_channeldto,channels)
            else:
                #将下级菜单放到临时集合中
                children.append(tmp_channeldto)
                #继续遍历下一级菜单
                self.recursive_list(state,channel.channel_pk,tmp_channeldto,channels)
        #将子菜单集合赋给当前的channel中的子菜单集合，[]集合也加进去，否则vue中会报错
        channeldto.children = children
        return channels